package com.thekernel.youdaoyun.service.impl;

import com.thekernel.youdaoyun.bean.Label;
import com.thekernel.youdaoyun.bean.Note;
import com.thekernel.youdaoyun.exception.MyException;
import com.thekernel.youdaoyun.exception.impl.UserNotLoginException;
import com.thekernel.youdaoyun.mapper.LabelMapper;
import com.thekernel.youdaoyun.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: thekernel
 * @Date: 2020/3/26 16:46
 * @Version: 1.0
 * @Description:
 */
@Service("LabelService")
public class LabelServiceImpl implements LabelService {

    @Autowired
    private LabelMapper labelMapper;

    /**
     * 添加标签
     *
     * @param label 标签(label, user_id)
     * @return -1: Data Not Found 0: Note Already Exists 1: Success
     */
    @Override
    public int addLabel(Label label) {
        if (label.getLabel().equals("") || label.getUserId() == 0) {
            return -1;
        }
        return this.labelMapper.add(label);
    }

    /**
     * 查询所有标签
     *
     * @param userId 用户id
     * @return 该用户的所有标签
     */
    @Override
    public Label[] queryLabels(long userId) throws MyException {
        if (userId == 0) {
            throw new UserNotLoginException();
        }
        return this.labelMapper.queryLabelsByUserId(userId);
    }

    /**
     * 查询笔记的所有标签
     *
     * @param noteId 笔记id
     * @return 该笔记的所有标签
     */
    @Override
    public Label[] queryNoteLabels(int noteId) {
        if (noteId == 0) {
            return null;
        }
        return this.labelMapper.queryLabelsByNoteId(noteId);
    }

    /**
     * 查询属于该标签的所有笔记
     *
     * @param labelId 标签id
     * @return 所有笔记
     * @throws MyException 用户未登录等
     */
    @Override
    public Note[] queryNotesByLabelId(int labelId) throws MyException {
        if (labelId == 0) {
            return null;
        }
        return this.labelMapper.queryNotesByLabelId(labelId);
    }
}
